package com.yxhl.zoume.core.car.presenter;

import android.content.Context;

import com.yxhl.protobuf.CommonRespParams;
import com.yxhl.protobuf.MpvModelRequest;
import com.yxhl.protobuf.MpvModelResponse;
import com.yxhl.protobuf.OrderBizType;
import com.yxhl.zoume.common.presenter.BasePresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.car.view.PricingRulesView;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.car.QueryPricingRulesUseCase;

import javax.inject.Inject;

/**
 * Author：zhongshan   
 * Date：2016/12/15 19:33      
 * Description：
 */
public class QueryPricingRulesPresenter extends BasePresenter {
    @Inject
    QueryPricingRulesUseCase useCase;

    private PricingRulesView pricingRulesView;

    @Inject
    public QueryPricingRulesPresenter(Context context) {
        super(context);
    }

    @Override
    public void attachView(BaseView view) {
        super.attachView(view);
        if (view instanceof PricingRulesView) {
            pricingRulesView = (PricingRulesView) view;
        }
    }

    public void queryPricingRules(OrderBizType bizType, int partTime) {

        MpvModelRequest.Builder builder = MpvModelRequest.newBuilder()
                .setCommonRequest(getCommonReqParams())
                .setBindType(bizType);
        if (partTime != 0)
            builder.setPartTime(partTime);

        useCase.execute(builder.build(),new ZMSubscriber<MpvModelResponse>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        pricingRulesView.showQueryLoading();
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        pricingRulesView.disMissQueryLoading();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        pricingRulesView.queryError();
                    }

                    @Override
                    public void onNext(MpvModelResponse mpvModelResponse) {
                        super.onNext(mpvModelResponse);
                        CommonRespParams commonResponse = mpvModelResponse.getCommonResponse();
                        if ("true".equals(commonResponse.getIsSucc())) {
                            pricingRulesView.queryPricingRulesSuccess(mpvModelResponse.getValuationRulesList());
                        } else {
                            pricingRulesView.queryPricingRulesFailure(commonResponse.getResultMsg());
                        }
                    }
                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        useCase.unsubscribe();
    }
}
